package com.example.fragmentdemo_229;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.LinearLayout;

import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private List<FragmentBase_229> fragmentList;
     private ViewPager viewPager;
     private BottomNavigationView bottomnv;
     private FragmentPagerAdapter fragmentPagerAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
        initViewPager();
        initBottomNV();
    }

    private void initViewPager() {
        viewPager=findViewById(R.id.viewPager);/*实例化viewPager*/
        /*实例化FragmnetPagerAdapter*/
        fragmentPagerAdapter=new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);/*取出第position个fragment*/
            }/*得到项目*/
            @Override
            public int getCount() {
                return fragmentList.size();
            }
        };/*得到数字*/


        viewPager.setAdapter(fragmentPagerAdapter);/*给一个控件设适配器*/

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset,
                                       int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                System.out.println("------------position="+position);
                switch (position){
                    case 0:
                        bottomnv.setSelectedItemId(R.id.key);break;
                    case 1:
                        bottomnv.setSelectedItemId(R.id.collect);break;
                    case 2:
                        bottomnv.setSelectedItemId(R.id.search);break;
                    case 3:
                        bottomnv.setSelectedItemId(R.id.owner);break;


                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {
                /*页面滚动状态改变*/

            }
        });

    }

    private void initBottomNV() {
       bottomnv=findViewById(R.id.bottomnv);
        /*实例化*/
        bottomnv.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                int vpid=0;
                switch (menuItem.getItemId()){
                    case R.id.key: vpid=0;break;
                    case R.id.collect: vpid=1;break;
                    case R.id.search: vpid=2;break;
                    case R.id.owner:vpid=3;break;
                }
                System.out.println("--------id="+vpid);
                viewPager.setCurrentItem(vpid);
                return true;
            }
        });


    }

    private void initViews() {
        fragmentList=new ArrayList<>();
        fragmentList.add(new Fragment1());
        fragmentList.add(new Fragment2());
        fragmentList.add(new Fragment3());
        fragmentList.add(new Fragment4());


    }
}
